
local assert     = assert
local require    = require
local bit        = bit
local coroutine  = coroutine
local debug      = debug
local io         = io
local pairs      = pairs
local ipairs     = ipairs
local math       = math
local os         = os
local print      = print
local pcall      = pcall
local xpcall     = xpcall
local rawget     = rawget
local rawset     = rawset
local select     = select
local string     = string
local table      = table
local tonumber   = tonumber
local tostring	 = tostring
local error      = error
local type       = type
local unpack     = unpack
local setmetatable = setmetatable
local getmetatable = getmetatable
local ngx = ngx

local string_char  = string.char
local string_sub   = string.sub
local string_byte  = string.byte
local string_gsub  = string.gsub
local string_find  = string.find
local string_upper = string.upper

local math_fmod   = math.fmod
local math_floor  = math.floor
local math_pow    = math.pow
local math_abs    = math.abs

local table_insert = table.insert

local os_date = os.date;
local table_concat = table.concat;


local uu         = require("modules.commhm.utils");
local log        = require("modules.commhm.log");
local ns_time    = require("modules.commhm.time");
local ns_network = require("modules.commhm.network");
local ns_utf8    = require("modules.commhm.utf8");


local CHtmlTable = require( "modules.html_page.CHtmlTable" )


local table_tostring = table.tostring
local table_loadstring = table.loadstring



local FOO = {
	_VERSION = '0.12.1',

	show_txt = {},
};


FOO.header = [[<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>create_index</title>
<script src="https://indevelop.mini1.cn:8088/oa/js_lib/cookie.min.js"></script>
<script src="https://indevelop.mini1.cn:8088/oa/js_lib/miniajax.js"></script>
</head>]];


--显示页面
FOO.show = function ( env_ )
	FOO.clean_txt()
	FOO.add_txt( FOO.header )
	FOO.add_txt( '<body>' )

	FOO.add_txt( '开发者资料查询页, 当前环境：' .. env_ .. ' <br> <hr>' )

	FOO.add_txt( '<table border=1 cellpadding=5>' )
	FOO.add_txt( '<tr>' )

	local active_style_ = 'style="background-color:#00ffff"'

	local txt_ = [[
<td><button @@1@@ onclick="change_env(1)" >开发环境1</button>&nbsp;</td>
<td><button @@2@@ onclick="change_env(2)" >先遣环境2</button>&nbsp;</td>
<td><button @@0@@ onclick="change_env(0)" ><font color="#FF0000"><b>正式环境0</b></font></button>&nbsp;
&nbsp;&nbsp;</td>
<td><button @@12@@ onclick="change_env(12)" >海外先遣环境12</button>&nbsp;</td>
<td><button @@10@@ onclick="change_env(10)" ><font color="#FF0000"><b>海外正式环境10</b></font></button>&nbsp;</td>
	]];

	for k, v in pairs( { 0, 1, 2, 10, 12 } ) do
		if  v == env_ then
			txt_ = string.gsub( txt_, "@@" .. v .. "@@", active_style_  )
		else
			txt_ = string.gsub( txt_, "@@" .. v .. "@@", ''  )
		end
	end


	FOO.add_txt( txt_ )

	FOO.add_txt( '</tr>' )
	FOO.add_txt( '</table>' )


	txt_ = [[

<br>
<br>
<br>

<table border=2 cellpadding=20>
<tr>
<td>
<br>
&nbsp;&nbsp; 查询指定迷你号: <input type=text id='_uin' style="height:20px; border:solid 1px;" value="" >
<button onclick="search_value(1)"> &nbsp;&nbsp;查 询&nbsp;&nbsp; </button>
<br>
<br>
&nbsp;&nbsp; 批量查询第几页: <input type=text id='_page' style="height:20px; border:solid 1px;" value="" >
<button onclick="search_value(2)"> &nbsp;&nbsp;查 询&nbsp;&nbsp; </button>
<br>
<br>
&nbsp;&nbsp; 批量查询地图wid列表(每行一个地图wid,最多50个):<br><br>
<textarea id="_wid" style="width:300px;height:300px;"></textarea>
<button onclick="search_value(3)"> &nbsp;&nbsp;查 询&nbsp;&nbsp; </button>

<br>
<br>
</td>
<td>
&nbsp;
</td>
<td>
<br>

地图批量隐藏和取消隐藏(每行一个地图wid，最多50个):<br>
精选，开发者，普通地图都可以<br>
<textarea id="_select_wid" style="width:300px;height:300px;"></textarea>
<br>
<br>
&nbsp;
<button onclick="search_value(4)"> &nbsp;&nbsp;批量隐藏&nbsp;&nbsp; </button>&nbsp;&nbsp;
<button onclick="search_value(5)"> &nbsp;&nbsp;批量取消隐藏&nbsp;&nbsp; </button>
<br>
<br>
处理结果：
<textarea id="_wid_ret" style="width:300px;height:300px;"></textarea>
<br>
<br>
</td>


</tr>
</table>


]];

	FOO.add_txt( txt_ )


	---js func
	txt_ = [[
<script type="text/javascript">

	document.getElementById("_uin").value = getCookie( "creator_uin" );
	document.getElementById("_page").value = getCookie( "creator_page" );
	document.getElementById("_wid").value = getCookie( "creator_wid" );

	function change_env(env_){
		var url_ = "";
		if  (env_ == 1) {
			url_ = "http://120.24.64.132:8087/miniw/map/?";
		}
		else if  (env_ == 2) {
			url_ = "http://211.159.183.137:8087/miniw/map/?";
		}
		else if (env_ == 0) {
			url_ = "http://shequ.mini1.cn:8087/miniw/map/?";
		}
		else if (env_ == 10) {
			url_ = "http://hwshequ.mini1.cn:8087/miniw/map/?";
		}
		else if (env_ == 12) {
			url_ = "http://47.75.147.207:8087/miniw/map/?";
		}
		else{
			alert("此环境还未部署此功能");
			return;
		}
		window.location.href= url_ + "time=1564737726&uin=1166200&auth=r&act=creator_read_action&action=index_page";
	}

	function search_value(type_) {
		if ( type_ == 1 ){
			var op_uin = document.getElementById("_uin").value;
			setCookie( "creator_uin", op_uin, "d30" );
			window.location.href= "/miniw/map/?time=1564739382&uin=1166200&auth=r&act=creator_read_action&action=scan_creator_by_page&op_uin=" + op_uin;
		}
		else if ( type_ == 2 ){
			var page = document.getElementById("_page").value;
			setCookie( "creator_page", page, "d30" );
			window.location.href= "/miniw/map/?time=1564739382&uin=1166200&auth=r&act=creator_read_action&action=scan_creator_by_page&limit=20&page=" + page;
		}
		else if ( type_ == 3 ){
			var wid_list = document.getElementById("_wid").value;
			setCookie( "creator_wid", wid_list, "d30" );

			var wid_list_varray = wid_list.split("\n");
			var wid_list_new = wid_list_varray.join(",");
			//alert( "wid_list_new=" + wid_list_new );
			window.location.href= "/miniw/map/?time=1564739382&uin=1166200&auth=r&act=creator_read_action&action=scan_creator_by_page&wid_list=" + wid_list_new;
		}
		else if ( type_ == 4 ){
			var wid_list = document.getElementById("_select_wid").value;
			setCookie( "creator_select_wid", wid_list, "d30" );

			var wid_list_varray = wid_list.split("\n");
			var wid_list_new = wid_list_varray.join(",");

			//alert( "wid_list_new=" + wid_list_new );
			//window.location.href= "/miniw/map/?time=1564739382&uin=1166200&confirm=1&auth=r&auth2=haima_set_select_map&act=creator_read_action&action=hide_select_map&wid_list=" + wid_list_new;

				var press_message = "即将批量隐藏，是否确定？";

				var press_yes = confirm(press_message);
				if (press_yes) {
					// post请求
					var post_data = { wid_list:wid_list_new };
					ajax({
						url: "/miniw/map/?time=1564739382&uin=1166200&confirm=1&auth=r&auth2=haima_set_select_map&act=creator_read_action&action=hide_select_map&post=1",
						type: "POST",
						data: post_data,
						dataType: "json",
						success: function (response) {
							if ( response.substring(0, 2) == 'ok' ){
								//alert(response.substring(2));
								document.getElementById("_wid_ret").value = response;
								//window.location.reload();
							}else{
								//alert( response );
								document.getElementById("_wid_ret").value = response;
							}
						},
						fail: function (status) {
							alert( "服务器被地星人抓走了，请稍后重试 ......" );
						}
					});
				};

		}
		else if ( type_ == 5 ){
			var wid_list = document.getElementById("_select_wid").value;
			setCookie( "creator_select_wid", wid_list, "d30" );

			var wid_list_varray = wid_list.split("\n");
			var wid_list_new = wid_list_varray.join(",");
			//alert( "wid_list_new=" + wid_list_new );
			//window.location.href= "/miniw/map/?time=1564739382&uin=1166200&confirm=1&auth=r&auth2=haima_set_select_map&act=creator_read_action&action=hide_select_map&cancel=1&wid_list=" + wid_list_new;

				var press_message = "即将批量取消隐藏，是否确定？";

				var press_yes = confirm(press_message);
				if (press_yes) {
					// post请求
					var post_data = { wid_list:wid_list_new };
					ajax({
						url: "/miniw/map/?time=1564739382&uin=1166200&confirm=1&auth=r&auth2=haima_set_select_map&act=creator_read_action&action=hide_select_map&cancel=1&post=1",
						type: "POST",
						data: post_data,
						dataType: "json",
						success: function (response) {
							if ( response.substring(0, 2) == 'ok' ){
								//alert(response.substring(2));
								document.getElementById("_wid_ret").value = response;
								//window.location.reload();
							}else{
								//alert( response );
								document.getElementById("_wid_ret").value = response;
							}
						},
						fail: function (status) {
							alert( "服务器被地星人抓走了，请稍后重试 ......" );
						}
					});
				};



		}
		else{

			alert( "ignore wid_list_new" );
		}
	}

</script>
]]

	FOO.add_txt( txt_ )

	FOO.add_txt( '</body></html>' )

	ngx.say( FOO.get_txt() )
end



----------------------------------------------------------

FOO.create_input_str = function( id_, value_, width_ )
	return '<input type=text id="' .. id_ .. '" style="width:' .. (width_ or 30)
			.. 'px; height:20px; border:solid 1px; text-align:center;" value="' .. value_ .. '" >'
end


FOO.create_button_str = function( function_name_, uin_, room_id_ )
	return '<button style="background-color:#00ffff"onclick="' .. function_name_ .. '(' .. uin_ .. ',' .. room_id_ .. ')" >submit</button>'
end


FOO.js_func = [[
<script type="text/javascript">
	function press_change( uin_, room_id_ ){
		var press_yes = confirm( "change " + uin_ + "_" + room_id_，\n\n是否确定？" );
		if (press_yes) {
			//post请求
			var post_data_ = { @{{post_data}} };
			ajax({
				url: "@{{url}}",
				type: "POST",
				data: post_data_,
				dataType: "json",
				success: function (response, xml) {
					alert( response );
				},
				fail: function (status) {
					alert( "服务器通讯失败，请联系海马 ......" );
				}
			});
		}
	}
</script>
]];



-----------------------------------------------------------
---清理输出
FOO.clean_txt = function()
	FOO.show_txt = {};
end

---增加输出内容
FOO.add_txt = function( txt_ )
	FOO.show_txt[ #FOO.show_txt + 1 ] = txt_
end


---获得所有输出
FOO.get_txt = function()
	return table_concat( FOO.show_txt )
end


return FOO;